﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace TripLOA.GUI.secure
{
    public partial class deleteAccount : TripLOA.GUI.internationalization.LOABasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {           

        }

        /// <summary>
        /// deletes user informations
        /// </summary>
        protected void but_delete_Click(object sender, EventArgs e)
        {
            if (!confirm_checkbox.Checked)
            {
                lab_status.Text = GetLocalResourceObject("NotConfirmed").ToString();
                return;
            }
                            
            if (Page.User.IsInRole("Administrators"))
            {
                lab_status.Text = GetLocalResourceObject("ErrorIsAdmin").ToString();
                return;
            }

            string username = Page.User.Identity.Name;

           //Password controls
            if (txt_pwd1.Text != null && txt_pwd1.Text != "" &&
                txt_pwd2.Text != null && txt_pwd2.Text != "" &&
                txt_pwd1.Text.Equals(txt_pwd2.Text)
                )
            {
                string password = txt_pwd1.Text;

                //checks if credentials are true
                bool flag = Membership.ValidateUser(username, password);

                if (!flag)
                {
                    lab_status.Text = GetLocalResourceObject("ErrorCredentials").ToString();
                    return;
                }

                if (Membership.DeleteUser(username, false))
                {
                    //Remove user from roles in order to prevent currently-logged deleted user to access secured sections
                    Roles.RemoveUserFromRoles(username, Roles.GetRolesForUser(username));

                    lab_status.Text = GetLocalResourceObject("UserDeleted").ToString();

                    //logging out user
                    Security.TripLoaUserManager.Instance.getUser(Membership.GetUser().UserName).signOut();
                    FormsAuthentication.SignOut();
                    Response.AddHeader("REFRESH","5;URL=/Default.aspx");
                }
                else lab_status.Text = GetLocalResourceObject("ErrorDelete").ToString();
            }
            else
            {
                lab_status.Text = GetLocalResourceObject("ErrorPassword").ToString();
            }
        }
    }
}
